package com.ck.controller.student;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.ck.domain.Exam;
import com.ck.domain.ExamBanji;
import com.ck.service.ExamService;
import com.ck.service.QuestionService;
import com.ck.service.TeacherService;

public class ShowExamInfoForStudentController extends AbstractController {
	
	private ExamService examService;
	private QuestionService questionService;
	private TeacherService teacherService;

	public void setExamService(ExamService examService) {
		this.examService = examService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		Integer examID = Integer.parseInt(request.getParameter("examID"));
		Exam exam = examService.gainExam(examID);
		
		String subject_name = questionService.gainSubject(exam.getSubject()).getName();
		String teacher_name = teacherService.gainTeacher(exam.getTeacher()).getTruename();
		
		List<ExamBanji> examBanjiList = examService.gainBanjiListForExam(examID);
		List<String> examBanjiNameList = new ArrayList();
		Iterator iter = examBanjiList.iterator();
		while(iter.hasNext()){
			ExamBanji examBanji = (ExamBanji) iter.next();
			Integer banjiID = examBanji.getBanji();
			String banjiName = teacherService.gainBanji(banjiID).getName();
			examBanjiNameList.add(banjiName);
		}
		
		Map model = new HashMap();
		model.put("exam", exam);
		model.put("subject_name", subject_name);
		model.put("teacher_name", teacher_name);
		model.put("examBanjiNameList", examBanjiNameList);
		
		return new ModelAndView("student/showExamInfoForStudent", model);
	}

}
